/*
 * Copyright (C) 2017 Gautam Chibde
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.chibde.visualizer;

import com.chibde.BaseVisualizer;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Custom component that creates a Circle and Bar visualizer effect for
 * <p>
 * Created by gautam chibde on 20/11/17.
 */
public class CircleBarVisualizer extends BaseVisualizer implements Component.DrawTask {
    private float[] points;
    private Paint circlePaint;
    private int radius;

    /**
     * constructor
     *
     * @param context
     */
    public CircleBarVisualizer(Context context) {
        super(context);
        addDrawTask(this::onDraw);
    }

    /**
     * constructor
     *
     * @param context
     * @param attrs
     */
    public CircleBarVisualizer(Context context,
                               AttrSet attrs) {
        super(context, attrs);
        addDrawTask(this::onDraw);
    }

    /**
     * constructor
     *
     * @param context context
     * @param attrs attrs
     * @param defStyleAttr defStyleAttr
     */
    public CircleBarVisualizer(Context context,
                               AttrSet attrs,
                               int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addDrawTask(this::onDraw);
    }

    @Override
    protected void init() {
        paint.setStyle(Paint.Style.STROKE_STYLE);
        circlePaint = new Paint();
        radius = -1;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (radius == -1) {
            radius = getHeight() < getWidth() ? getHeight() : getWidth();
            radius = (int) (radius * 0.65 / 2);
            double circumference = 2 * Math.PI * radius;
            paint.setStrokeWidth((float) (circumference / 120));
            circlePaint.setStyle(Paint.Style.STROKE_STYLE);
            circlePaint.setStrokeWidth(4);
        }
        circlePaint.setColor(new Color(color));
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, circlePaint);
        if (bytes != null) {
            if (points == null || points.length < bytes.length * 4) {
                points = new float[bytes.length * 4];
            }
            double angle = 0;
            for (int i = 0; i < 120; i++, angle += 3) {
                int x = (int) Math.ceil(i * 8.5);
                int t = ((byte) (-Math.abs(bytes[x]) + 128)) * (getHeight() / 4) / 128;
                points[i * 4] = (float) (getWidth() / 2 + radius * Math.cos(Math.toRadians(angle)));
                points[i * 4 + 1] = (float) (getHeight() / 2 + radius * Math.sin(Math.toRadians(angle)));
                points[i * 4 + 2] = (float) (getWidth() / 2 + (radius + t) * Math.cos(Math.toRadians(angle)));
                points[i * 4 + 3] = (float) (getHeight() / 2 + (radius + t) * Math.sin(Math.toRadians(angle)));
            }
            canvas.drawLines(points, paint);
        }
    }
}
